// pages/datailSwoole/datailSwoole.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    webSocket:'',
    message:'',
    userId:'',
    brokers_id:'',
    list:[],
    value:'',
    userImage:'',
    brokerImg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   var brokers_id=options.brokers_id
   this.setData({
    brokers_id
   })
   this.setData({
     brokerImg:wx.getStorageSync('brokerImg')
   })
   
   wx.request({
    url: 'http://www.house.com/index.php/api/userToken',
    header:{
      token:wx.getStorageSync('token')
    },
    method:"POST",
    success:res=>{
      if(res.data.code==200){
         this.setData({
         userId:res.data.data.uid,
         userImage:res.data.data.userImage
         })
         this.sendInfo('open')
      }
    }
  })

  /**
   * 建立连接
   */
    var webSocket=wx.connectSocket({
      url: 'ws://81.68.163.7:9505',
    })
    this.setData({
      webSocket
    })
  
   
    /**
     * 处理信息
     */
    wx.onSocketMessage((result) => {
      let list = this.data.list;
      var data=JSON.parse(result.data)
      let newList = list.concat(data)
      this.setData({
        list:newList
      })

    })
  },


  /**
   * 发送信息
   */
  send(){
    this.sendInfo('send',this.data.message)
  },

  /**
   * 方法封装
   */
  sendInfo(type,message=''){
    var message={
      uid:this.data.userId,
      tid:this.data.brokers_id,
      data:message,
      type:type
    }
    var Message=JSON.stringify(message)
    wx.sendSocketMessage({
      data: Message,
      success:res=>{
       this.setData({
        value:''
       })
      }
    })
  },

  /**
   * 
   * 发送的信息
   */
  content(e){
    var message= e.detail.value
    this.setData({
      message
    })
  }

  
})